package com.lgCore.provide.service.impl;

import com.alibaba.fastjson.JSON;
import com.lgCore.model.ServiceInfaceData;
import com.lgCore.model.ServiceInfaceDetailData;
import com.lgCore.model.ZKNodeType;
import com.lgCore.provide.model.ServiceZkData;
import com.lgCore.provide.service.ICommonServiceServer;
import com.lgCore.util.ZkUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.CloseableUtils;
import org.apache.zookeeper.CreateMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author ligang
 * <p>
 * service 层对zk的操作
 */
public class CommonServiceServerImpl implements ICommonServiceServer {
    private static final Logger LOGGER = LoggerFactory.getLogger(CommonServiceServerImpl.class);
    //rpc根目录名称
    private String zkRootPath;
    /**
     * corator
     */
    private CuratorFramework client;

    public CommonServiceServerImpl() {
    }

    public void initConfig(String zkRootPath, CuratorFramework client) {
        this.zkRootPath = zkRootPath;
        this.client = client;
    }

    @Override
    public void close() throws Exception {
        CloseableUtils.closeQuietly(this.client);
    }

    @Override
    public void registerServer(List<ServiceZkData> serviceZkDataList) throws Exception {
        //根目录入口
        if (serviceZkDataList != null && !serviceZkDataList.isEmpty()) {
            for (ServiceZkData serviceZkData : serviceZkDataList) {

                String childPath = "";

                //运行环境（dev test prod）
                if (StringUtils.isNotBlank(serviceZkData.getProfilesActive())) {
                    childPath = "/" + this.zkRootPath + "/" + serviceZkData.getProfilesActive();
                    //工作环境目录，记录接口信息
                    ServiceInfaceData serviceInfaceData = new ServiceInfaceData();
                    serviceInfaceData.setZkNodeType(ZKNodeType.PROFILESACTIVE);
                    ZkUtil.addNode(this.client, childPath, JSON.toJSONString(serviceInfaceData), CreateMode.PERSISTENT);
                }

                //工作环境
                if (StringUtils.isNotBlank(serviceZkData.getRpcSystemRegion())) {
                    childPath = childPath + "/" + serviceZkData.getRpcSystemRegion();
                    //工作环境目录，记录接口信息
                    ServiceInfaceData serviceInfaceData = new ServiceInfaceData();
                    serviceInfaceData.setZkNodeType(ZKNodeType.REGION);
                    ZkUtil.addNode(this.client, childPath, JSON.toJSONString(serviceInfaceData), CreateMode.PERSISTENT);
                }
                //工作接口目录，记录接口信息
                childPath = childPath + "/" + serviceZkData.getServicePath();
                ServiceInfaceData serviceInfaceData = new ServiceInfaceData();
                serviceInfaceData.setZkNodeType(ZKNodeType.INTERFACE);
                serviceInfaceData.setServicePath(serviceZkData.getServicePath());
                ZkUtil.addNode(this.client, childPath, JSON.toJSONString(serviceInfaceData), CreateMode.PERSISTENT, true);
                //工作子目录信息，记录ip和端口
                ServiceInfaceDetailData serviceInfaceDetailData = new ServiceInfaceDetailData();
                serviceInfaceDetailData.setIp(serviceZkData.getIp());
                serviceInfaceDetailData.setNettyPort(serviceZkData.getNettyPort());
                serviceInfaceDetailData.setServicePath(serviceZkData.getServicePath());
                serviceInfaceDetailData.setZkNodeType(ZKNodeType.INTERFACE_DETAIL);
                childPath = childPath + "/" + serviceZkData.getIp() + ":" + serviceZkData.getNettyPort();
                ZkUtil.addNode(this.client, childPath, JSON.toJSONString(serviceInfaceDetailData), CreateMode.EPHEMERAL_SEQUENTIAL, true);
            }
        }
    }

    @Override
    public void delServer(List<ServiceZkData> serviceZkDataList) throws Exception {
        //根目录入口
        if (serviceZkDataList != null && !serviceZkDataList.isEmpty()) {
            for (ServiceZkData serviceZkData : serviceZkDataList) {
                try{
                    String childPath = "/" + this.zkRootPath + "/" + serviceZkData.getProfilesActive() +
                            "/" + serviceZkData.getRpcSystemRegion() + "/" + serviceZkData.getServicePath() +
                            "/" + serviceZkData.getIp() + ":" + serviceZkData.getNettyPort();
                    ZkUtil.delNode(this.client,childPath);
                }catch (Exception e){
                    LOGGER.error("节点删除失败");
                }

            }
        }
    }

}
